package com.buddy.sds.auth.controller;

import com.buddy.sds.auth.entity.PermissionTree;
import com.buddy.sds.auth.entity.PermissionTypeEnum;
import com.buddy.sds.auth.entity.SystemPermission;
import com.buddy.sds.auth.service.ISystemPermissionService;
import com.buddy.sds.common.RestApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/auth/permission")
public class SystemPermissionController {

    @Autowired
    private ISystemPermissionService permissionService;

    /**
     * 获取系统内所有权限树
     * @return
     */
    @GetMapping("/getPermissionTree")
    public RestApiResponse getPermissionTree() {
        List<PermissionTree> permissionTreeList = permissionService.getPermissionTree(true);
        return new RestApiResponse(0, "", permissionTreeList);
    }

    @DeleteMapping("/deletePermission")
    public RestApiResponse deletePermission(@RequestParam("permissionId")String pid){

        permissionService.deleteCascade(pid);

        return new RestApiResponse(0,"删除成功",null);
    }

    @GetMapping("/getPermission")
    public RestApiResponse getPermission(@RequestParam("permissionId")String pid){

        SystemPermission permission = permissionService.getById(pid);

        if(permission != null){
            return new RestApiResponse(0,"",permission);
        }else{
            return new RestApiResponse(-1,"权限信息不存在",null);
        }
    }

    @PutMapping("/toggleVisible")
    public RestApiResponse toggleVisible(@RequestParam("permissionId")String pid){

        boolean flag = permissionService.toggleVisible(pid);

        return new RestApiResponse(flag ? 0 : -1 ,"",null);
    }

    @PutMapping("/toggleAuth")
    public RestApiResponse toggleAuth(@RequestParam("permissionId")String pid){

        boolean flag = permissionService.toggleAuth(pid);

        return new RestApiResponse(flag ? 0 : -1 ,"",null);
    }

    @PutMapping("/updatePermission")
    public RestApiResponse updatePermission(@RequestBody SystemPermission permission){

        int status = 0;

        String message = "";

        do{
            if(StringUtils.isEmpty(permission.getPermissionId())){
                status = -1;
                message = "缺少权限ID";
                break;
            }

            SystemPermission oldPermission = permissionService.getById(permission.getPermissionId());

            if(oldPermission == null){
                status = -2;
                message = "权限ID错误";
                break;
            }


            if(StringUtils.isEmpty(permission.getPermissionName())){
                status = -1;
                message = "缺少权限名称";
                break;
            }

            //权限类型和PID不允许修改，直接用旧的赋值

            permission.setPermissionPid(oldPermission.getPermissionPid());
            permission.setPermissionType(oldPermission.getPermissionType());


            if(StringUtils.isEmpty(permission.getPermissionKey())){
                status = -1;
                message = "缺少权限关键字";
                break;
            }


            if(permission.getPermissionType() != PermissionTypeEnum.MENU){


                if(StringUtils.isEmpty(permission.getPermissionUrl())){
                    status = -1;
                    message = "缺少权限路径/API接口地址";
                    break;
                }


                if(permission.getPermissionType() == PermissionTypeEnum.FUNCTION){

                    //功能路由前面不要绝对路径
                    //把开头的/去掉
                    permission.setPermissionUrl(permission.getPermissionUrl().replaceFirst("/",""));


                    //检查是不是设置为主页了

                    if(permission.isPermissionHomepage()){
                        //设为首页之前先移除现有主页

                        permissionService.removeHomePage();
                    }


                }

            }

            if(permissionService.updateById(permission)){
                status = 0;
                message = "添加成功";
            }
        }while (false);

        return new RestApiResponse(status,message,null);
    }

    @PostMapping("/addPermission")
    public RestApiResponse addPermission(@RequestBody SystemPermission permission){
        int status = 0;
        String message = "";

        do{
            if(StringUtils.isEmpty(permission.getPermissionName())){
                status = -1;
                message = "缺少权限名称";
                break;
            }

            if(permission.getPermissionType() == null){
                status = -1;
                message = "缺少权限类型";
                break;
            }

            if(StringUtils.isEmpty(permission.getPermissionPid())){

                permission.setPermissionPid("ROOT");
            }

            if(permission.getPermissionType() != PermissionTypeEnum.MENU){


                if(StringUtils.isEmpty(permission.getPermissionUrl())){
                    status = -1;
                    message = "缺少权限路径/API接口地址";
                    break;
                }

                if(StringUtils.isEmpty(permission.getPermissionKey())){
                    status = -1;
                    message = "缺少权限关键字";
                    break;
                }

                if(permission.getPermissionType() == PermissionTypeEnum.FUNCTION){

                    //功能路由前面不要绝对路径
                    //把开头的/去掉
                    permission.setPermissionUrl(permission.getPermissionUrl().replaceFirst("/",""));

                    //检查是不是设置为主页了

                    if(permission.isPermissionHomepage()){
                        //设为首页之前先移除现有主页
                        permissionService.removeHomePage();
                    }
                }
            }

            if(permissionService.save(permission)){
                status = 0;
                message = "添加成功";
            }

        }while (false);

        return new RestApiResponse(status,message,null);
    }

}
